import Mock from 'mockjs'

// 模拟分类数据
const categories = [
  // 一级分类
  { id: 1, name: '掼蛋练习库', description: '掼蛋专业技巧训练', parentId: 0, style: 'style-A', sort: 1, questionNum: 15 },
  // 二级分类
  { id: 2, name: '组牌', description: '学习如何组合最优手牌', parentId: 1, style: 'style-B', sort: 1, questionNum: 8 },
  { id: 3, name: '开牌', description: '学习首次出牌的策略', parentId: 1, style: 'style-C', sort: 2, questionNum: 7 },
  // 另一个一级分类
  { id: 4, name: '数学', description: '从基础到高等数学', parentId: 0, style: 'style-A', sort: 2, questionNum: 20 },
  { id: 5, name: '代数', description: '代数学习', parentId: 4, style: 'style-B', sort: 1, questionNum: 10 },
  { id: 6, name: '几何', description: '几何学习', parentId: 4, style: 'style-C', sort: 2, questionNum: 10 }
];

// 类型列表 (分页)
Mock.mock(/\/api\/web\/category\/pageQueryCategoryList/, 'post', (options) => {
  const { pageNum = 1, pageSize = 10 } = JSON.parse(options.body)
  const list = categories.slice((pageNum - 1) * pageSize, pageNum * pageSize)
  return {
    code: '200',
    resData: {
      total: categories.length,
      list: list,
      pageNum: pageNum,
      pageSize: pageSize
    },
    msg: '查询成功'
  }
})

// 一级类型列表
Mock.mock(/\/api\/web\/category\/getTopCategoryList/, 'post', () => {
  return {
    code: '200',
    resData: categories.filter(c => c.parentId === 0).map(c => ({ id: c.id, name: c.name })),
    msg: '查询成功'
  }
})

// 二级类型列表
Mock.mock(/\/api\/web\/category\/getSubCategoryList/, 'post', (options) => {
  const { parentId } = JSON.parse(options.body)
  return {
    code: '200',
    resData: categories.filter(c => c.parentId === parentId),
    msg: '查询成功'
  }
})

// 添加类型
Mock.mock(/\/api\/web\/category\/addCategory/, 'post', (options) => {
    const data = JSON.parse(options.body)
    const newCategory = {
        id: Mock.Random.id(),
        name: data.categoryName,
        description: data.description,
        parentId: data.parentId || 0,
        style: data.style,
        sort: categories.length + 1,
        questionNum: 0
    }
    categories.push(newCategory)
    return {
        code: '200',
        resData: null,
        msg: '添加成功'
    }
})

// 删除类型
Mock.mock(/\/api\/web\/category\/deleteCategory/, 'post', (options) => {
    const { categoryId } = JSON.parse(options.body)
    const index = categories.findIndex(c => c.id === categoryId)
    if (index !== -1) {
        categories.splice(index, 1)
    }
    return {
        code: '200',
        resData: null,
        msg: '删除成功'
    }
})

// 修改类型
Mock.mock(/\/api\/web\/category\/updateCategory/, 'post', (options) => {
    const { categoryId, categoryName, style } = JSON.parse(options.body)
    const category = categories.find(c => c.id === categoryId)
    if (category) {
        category.name = categoryName
        category.style = style
    }
    return {
        code: '200',
        resData: null,
        msg: '修改成功'
    }
})